********************************************************************************
******"Macroprudential Regulation, Quantitative Easing, and Bank Lending"*******
********************************************************************************

********************************************************************************
********************************************************************************
**********************************2019******************************************
********************************************************************************
********************************************************************************
global y "taeg_w199"
global ed4top "dT1dtop66e14 dT2dtop66e14 dT3dtop66e14 dT4dtop66e14 dT5dtop66e14 dT7dtop66e14 dT8dtop66e14 dT9dtop66e14 dT10dtop66e14 dT11dtop66e14 dT12dtop66e14"
global ed4mid "dT1dmid6633e14 dT2dmid6633e14 dT3dmid6633e14 dT4dmid6633e14 dT5dmid6633e14 dT7dmid6633e14 dT8dmid6633e14 dT9dmid6633e14 dT10dmid6633e14 dT11dmid6633e14 dT12dmid6633e14"
global controls "lncrefin dT*exlnassets dT*excashas"
global regcommands ", pool(5) compact absorb(bankid borrowerid#time) vce(cluster bankid)"

clear *
**# Bookmark 1 (input)
import sas using $inputdirectory/taeg2019ana, case(lower)

duplicates drop data_oss cod_oa censito_crittog cntrct_id instrmnt_id, force
rename ente_segn bankid
rename censito_crittog borrowerid
format borrowerid %20.0g

**# Bookmark 1 (input)
merge m:1 bankid using $inputdirectory/INB_gru2019, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.
drop bankid
rename gru bankid

replace data_oss=data_oss*1000
gen xxx=dofc(data_oss)
gen time=mofd(xxx)
format time %tm
drop data_oss
keep if time>=tm(2019m3)&time<=tm(2020m2)

collapse (sum) acc taeg_acc, by(bankid borrowerid time)

**# Bookmark 1 (input)
merge m:1 bankid using $inputdirectory/INB_preann2019m8, keepus(dtop66e14 dmid6633e14 exlnassets excashas)
keep if _merge==3
drop _merge

**# Bookmark 1 (input)
merge m:1 bankid time using $inputdirectory/INB_refinm2019, keepus(lncrefin)
keep if _merge!=2
drop _merge

gen taeg=100*taeg_acc/acc
winsor2 taeg if taeg!=., suffix(_w199) cuts(1 99)

run RO_tdummies 2019 12
run RO_tdintera 12 "dtop66e14 dmid6633e14 exlnassets excashas"

**# Bookmark 1 (output - column 1)
cd $outputdirectory
reghdfe $y $ed4top $ed4mid $controls $regcommands
est save TABIVB_C12
cd ..


********************************************************************************
********************************************************************************
**********************************2015******************************************
********************************************************************************
********************************************************************************
global y "taeg_w199"
global regcommands ", pool(5) compact absorb(bankid borrowerid#time) vce(cluster bankid)"

clear *
**# Bookmark (input)
import sas ENTE_SEGN CENSITO_CRITTOG DATA_OSS VOCESOTVOC VALORE DURATA DURORI_TASSO TIP_ATT using $inputdirectory/taeg, case(lower)

rename ente_segn bankid
rename censito_crittog borrowerid
format borrowerid %20.0g

reshape wide valore, i(data_oss bankid borrowerid durata durori_tasso tip_att) j(vocesotvoc)

rename valore5504213 taeg
rename valore5504233 amount
replace taeg=taeg/10000

replace data_oss=data_oss*1000
gen xxx=dofc(data_oss)
gen time=qofd(xxx)
format time %tq
drop data_oss
drop xxx

**# Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_gru2015, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.
drop bankid
rename gru bankid

gen xxx=taeg*amount
collapse (sum) xxx amount, by(bankid borrowerid time)

**# Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2014m12, keepus(dtop85e13 exlnassets excashas)
keep if _merge==3
drop _merge

**# Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_refinq2015
keep if _merge!=2
drop _merge
recode lncrefin (.=0)

keep if time>=tq(2014q3)
keep if time<=tq(2015q2)

egen id=group(bankid borrowerid)
xtset id time

gen dT1=(time==tq(2014q3))
gen dT3=(time==tq(2015q1))
gen dT4=(time==tq(2015q2))

gen taeg=xxx/amount
drop xxx
winsor2 taeg if taeg!=., suffix(_w199) cuts(1 99)

foreach x of numlist 1 3 4 {
foreach var of varlist excashas exlnassets dtop85e13 {
gen dT`x'`var'=dT`x'*`var'
}
}

**# Bookmark (outout - column 2)
cd $outputdirectory
est clear
reghdfe $y dT1dtop85e13 dT3dtop85e13 dT4dtop85e13 dT1exlnassets dT3exlnassets dT4exlnassets dT1excashas dT3excashas dT4excashas lncrefin $regcommands
est save TABIVB_C12, append
cd ..

